package com.jxw.launcher.util;

import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import net.lingala.zip4j.util.InternalZipConstants;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.json.JSONObject;

/* loaded from: classes.dex */
public class OkHttpDownUtil {
    private static final String TAG = "OkHttpDownUtil";
    private static volatile OkHttpDownUtil mInstance;
    private Call mCall;
    private String mDownUrl;
    private HttpDownListener mHttpDownListener;
    private JSONObject mJson;
    private File mPath;
    private long mAlreadyDownLength = 0;
    private long mTotalLength = 0;
    private int mSign = 0;

    private OkHttpDownUtil() {
    }

    public static OkHttpDownUtil getInstance() {
        if (mInstance == null) {
            synchronized (OkHttpDownUtil.class) {
                if (mInstance == null) {
                    mInstance = new OkHttpDownUtil();
                }
            }
        }
        return mInstance;
    }

    public void getDownRequest(String str, File file, HttpDownListener httpDownListener) {
        this.mSign = 1;
        this.mDownUrl = str;
        this.mPath = file;
        this.mHttpDownListener = httpDownListener;
        this.mAlreadyDownLength = 0L;
        this.mCall = new OkHttpClient().newCall(new Request.Builder().url(this.mDownUrl).get().build());
        this.mCall.enqueue(new Callback() { // from class: com.jxw.launcher.util.OkHttpDownUtil.1
            @Override // okhttp3.Callback
            public void onFailure(Call call, IOException iOException) {
                if (OkHttpDownUtil.this.mHttpDownListener != null) {
                    OkHttpDownUtil.this.mHttpDownListener.onFailure(call, iOException);
                }
            }

            @Override // okhttp3.Callback
            public void onResponse(Call call, Response response) throws IOException {
                ResponseBody body = response.body();
                OkHttpDownUtil.this.mTotalLength = body.contentLength();
                InputStream byteStream = body.byteStream();
                FileOutputStream fileOutputStream = new FileOutputStream(OkHttpDownUtil.this.mPath);
                try {
                    try {
                        byte[] bArr = new byte[2048];
                        while (true) {
                            int read = byteStream.read(bArr);
                            if (read == -1) {
                                break;
                            }
                            OkHttpDownUtil.this.mAlreadyDownLength += read;
                            fileOutputStream.write(bArr, 0, read);
                            if (OkHttpDownUtil.this.mHttpDownListener != null) {
                                OkHttpDownUtil.this.mHttpDownListener.onResponse(call, response, OkHttpDownUtil.this.mTotalLength, OkHttpDownUtil.this.mAlreadyDownLength);
                            }
                        }
                    } catch (Exception unused) {
                        Log.e(OkHttpDownUtil.TAG, "Get下载异常");
                    }
                } finally {
                    fileOutputStream.close();
                    byteStream.close();
                    Log.e(OkHttpDownUtil.TAG, "流关闭");
                }
            }
        });
    }

    public void getRenewalDownRequest(String str, File file, HttpDownListener httpDownListener) {
        this.mSign = 2;
        this.mDownUrl = str;
        this.mPath = file;
        this.mHttpDownListener = httpDownListener;
        Request.Builder url = new Request.Builder().url(this.mDownUrl);
        this.mCall = new OkHttpClient().newCall(url.header("RANGE", "bytes=" + this.mAlreadyDownLength + "-").build());
        this.mCall.enqueue(new Callback() { // from class: com.jxw.launcher.util.OkHttpDownUtil.2
            @Override // okhttp3.Callback
            public void onFailure(Call call, IOException iOException) {
                if (OkHttpDownUtil.this.mHttpDownListener != null) {
                    OkHttpDownUtil.this.mHttpDownListener.onFailure(call, iOException);
                }
                Log.e(OkHttpDownUtil.TAG, "onFailure: 异常报错=" + iOException.toString());
            }

            @Override // okhttp3.Callback
            public void onResponse(Call call, Response response) throws IOException {
                StringBuilder sb;
                ResponseBody body = response.body();
                InputStream byteStream = body.byteStream();
                RandomAccessFile randomAccessFile = new RandomAccessFile(OkHttpDownUtil.this.mPath, InternalZipConstants.WRITE_MODE);
                if (OkHttpDownUtil.this.mTotalLength == 0) {
                    OkHttpDownUtil.this.mTotalLength = body.contentLength();
                    randomAccessFile.setLength(OkHttpDownUtil.this.mTotalLength);
                }
                if (OkHttpDownUtil.this.mAlreadyDownLength != 0) {
                    randomAccessFile.seek(OkHttpDownUtil.this.mAlreadyDownLength);
                }
                byte[] bArr = new byte[2048];
                while (true) {
                    try {
                        try {
                            int read = byteStream.read(bArr);
                            if (read == -1) {
                                break;
                            }
                            randomAccessFile.write(bArr, 0, read);
                            OkHttpDownUtil.this.mAlreadyDownLength += read;
                            if (OkHttpDownUtil.this.mHttpDownListener != null) {
                                OkHttpDownUtil.this.mHttpDownListener.onResponse(call, response, OkHttpDownUtil.this.mTotalLength, OkHttpDownUtil.this.mAlreadyDownLength);
                            }
                        } catch (Exception unused) {
                            Log.e(OkHttpDownUtil.TAG, "Get下载异常");
                            OkHttpDownUtil.this.mAlreadyDownLength = randomAccessFile.getFilePointer();
                            randomAccessFile.close();
                            byteStream.close();
                            sb = new StringBuilder();
                        }
                    } catch (Throwable th) {
                        OkHttpDownUtil.this.mAlreadyDownLength = randomAccessFile.getFilePointer();
                        randomAccessFile.close();
                        byteStream.close();
                        Log.e(OkHttpDownUtil.TAG, "流关闭 下载的位置=" + OkHttpDownUtil.this.mAlreadyDownLength);
                        throw th;
                    }
                }
                OkHttpDownUtil.this.mAlreadyDownLength = randomAccessFile.getFilePointer();
                randomAccessFile.close();
                byteStream.close();
                sb = new StringBuilder();
                sb.append("流关闭 下载的位置=");
                sb.append(OkHttpDownUtil.this.mAlreadyDownLength);
                Log.e(OkHttpDownUtil.TAG, sb.toString());
            }
        });
    }

    public void postDownRequest(String str, File file, JSONObject jSONObject, HttpDownListener httpDownListener) {
        this.mSign = 3;
        this.mDownUrl = str;
        this.mPath = file;
        this.mJson = jSONObject;
        this.mHttpDownListener = httpDownListener;
        this.mAlreadyDownLength = 0L;
        this.mCall = new OkHttpClient().newCall(new Request.Builder().url(this.mDownUrl).post(changeJSON(jSONObject)).build());
        this.mCall.enqueue(new Callback() { // from class: com.jxw.launcher.util.OkHttpDownUtil.3
            @Override // okhttp3.Callback
            public void onFailure(Call call, IOException iOException) {
                if (OkHttpDownUtil.this.mHttpDownListener != null) {
                    OkHttpDownUtil.this.mHttpDownListener.onFailure(call, iOException);
                }
            }

            @Override // okhttp3.Callback
            public void onResponse(Call call, Response response) throws IOException {
                ResponseBody body = response.body();
                OkHttpDownUtil.this.mTotalLength = body.contentLength();
                InputStream byteStream = body.byteStream();
                FileOutputStream fileOutputStream = new FileOutputStream(OkHttpDownUtil.this.mPath);
                byte[] bArr = new byte[2048];
                while (true) {
                    try {
                        try {
                            int read = byteStream.read(bArr);
                            if (read == -1) {
                                break;
                            }
                            fileOutputStream.write(bArr, 0, read);
                            OkHttpDownUtil.this.mAlreadyDownLength += read;
                            if (OkHttpDownUtil.this.mHttpDownListener != null) {
                                OkHttpDownUtil.this.mHttpDownListener.onResponse(call, response, OkHttpDownUtil.this.mTotalLength, OkHttpDownUtil.this.mAlreadyDownLength);
                            }
                        } catch (Exception unused) {
                            Log.e(OkHttpDownUtil.TAG, "Post下载异常");
                        }
                    } finally {
                        fileOutputStream.close();
                        byteStream.close();
                        Log.e(OkHttpDownUtil.TAG, "流关闭");
                    }
                }
            }
        });
    }

    public void postRenewalDownRequest(String str, File file, JSONObject jSONObject, HttpDownListener httpDownListener) {
        this.mSign = 4;
        this.mDownUrl = str;
        this.mPath = file;
        this.mJson = jSONObject;
        this.mHttpDownListener = httpDownListener;
        Request.Builder url = new Request.Builder().url(this.mDownUrl);
        this.mCall = new OkHttpClient().newCall(url.header("RANGE", "bytes=" + this.mAlreadyDownLength + "-").post(changeJSON(jSONObject)).build());
        this.mCall.enqueue(new Callback() { // from class: com.jxw.launcher.util.OkHttpDownUtil.4
            @Override // okhttp3.Callback
            public void onFailure(Call call, IOException iOException) {
                if (OkHttpDownUtil.this.mHttpDownListener != null) {
                    OkHttpDownUtil.this.mHttpDownListener.onFailure(call, iOException);
                }
            }

            @Override // okhttp3.Callback
            public void onResponse(Call call, Response response) throws IOException {
                StringBuilder sb;
                ResponseBody body = response.body();
                InputStream byteStream = body.byteStream();
                RandomAccessFile randomAccessFile = new RandomAccessFile(OkHttpDownUtil.this.mPath, InternalZipConstants.WRITE_MODE);
                if (OkHttpDownUtil.this.mTotalLength == 0) {
                    OkHttpDownUtil.this.mTotalLength = body.contentLength();
                    randomAccessFile.setLength(OkHttpDownUtil.this.mTotalLength);
                }
                if (OkHttpDownUtil.this.mAlreadyDownLength != 0) {
                    randomAccessFile.seek(OkHttpDownUtil.this.mAlreadyDownLength);
                }
                byte[] bArr = new byte[2048];
                while (true) {
                    try {
                        try {
                            int read = byteStream.read(bArr);
                            if (read == -1) {
                                break;
                            }
                            randomAccessFile.write(bArr, 0, read);
                            OkHttpDownUtil.this.mAlreadyDownLength += read;
                            if (OkHttpDownUtil.this.mHttpDownListener != null) {
                                OkHttpDownUtil.this.mHttpDownListener.onResponse(call, response, OkHttpDownUtil.this.mTotalLength, OkHttpDownUtil.this.mAlreadyDownLength);
                            }
                        } catch (Exception unused) {
                            Log.e(OkHttpDownUtil.TAG, "Post下载异常");
                            OkHttpDownUtil.this.mAlreadyDownLength = randomAccessFile.getFilePointer();
                            randomAccessFile.close();
                            byteStream.close();
                            sb = new StringBuilder();
                        }
                    } catch (Throwable th) {
                        OkHttpDownUtil.this.mAlreadyDownLength = randomAccessFile.getFilePointer();
                        randomAccessFile.close();
                        byteStream.close();
                        Log.e(OkHttpDownUtil.TAG, "流关闭 下载的位置=" + OkHttpDownUtil.this.mAlreadyDownLength);
                        throw th;
                    }
                }
                OkHttpDownUtil.this.mAlreadyDownLength = randomAccessFile.getFilePointer();
                randomAccessFile.close();
                byteStream.close();
                sb = new StringBuilder();
                sb.append("流关闭 下载的位置=");
                sb.append(OkHttpDownUtil.this.mAlreadyDownLength);
                Log.e(OkHttpDownUtil.TAG, sb.toString());
            }
        });
    }

    public void resume() {
        int i = this.mSign;
        if (i == 0) {
            return;
        }
        if (i == 1) {
            getDownRequest(this.mDownUrl, this.mPath, this.mHttpDownListener);
        } else if (i == 2) {
            getRenewalDownRequest(this.mDownUrl, this.mPath, this.mHttpDownListener);
        } else if (i == 3) {
            postDownRequest(this.mDownUrl, this.mPath, this.mJson, this.mHttpDownListener);
        } else if (i != 4) {
        } else {
            postRenewalDownRequest(this.mDownUrl, this.mPath, this.mJson, this.mHttpDownListener);
        }
    }

    public void stop() {
        Call call = this.mCall;
        if (call != null) {
            call.cancel();
        }
    }

    public void deleteCurrentFile() {
        File file = this.mPath;
        if (file == null) {
            Log.e(TAG, "deleteCurrentFile error : 没有路径");
        } else if (!file.exists()) {
            Log.e(TAG, "deleteCurrentFile error: 文件不存在");
        } else {
            this.mPath.delete();
            this.mAlreadyDownLength = 0L;
            this.mTotalLength = 0L;
            this.mSign = 0;
        }
    }

    public void destroy() {
        Call call = this.mCall;
        if (call != null) {
            call.cancel();
            this.mCall = null;
        }
        this.mSign = 0;
        this.mDownUrl = null;
        this.mPath = null;
        this.mHttpDownListener = null;
        this.mAlreadyDownLength = 0L;
        this.mTotalLength = 0L;
    }

    private RequestBody changeJSON(JSONObject jSONObject) {
        return FormBody.create(MediaType.parse("application/json; charset=utf-8"), String.valueOf(jSONObject));
    }
}
